package ui;

import Service.AccountService;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MainFrame extends JFrame implements ActionListener {
    private JDesktopPane desktopPane ; //虚拟桌面容器
    public MainFrame(String name){
        setVisible(true);
        setTitle("图书管理系统，当前用户【"+name+"】");
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setBounds(100,100,634,464);
        JPanel conenetPane = new JPanel();
        conenetPane.setBorder(new EmptyBorder(5,5,5,5));
        setContentPane(conenetPane);//设置当前页面需要展示的主面板
        conenetPane.setLayout(new BorderLayout(0,0));
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {


                int result = JOptionPane.showConfirmDialog(MainFrame.this,"确定退出系统？","系统提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                if(result == JOptionPane.YES_OPTION){
                    MainFrame.this.dispose();
                    new LoginFrame();
                }else if(result == JOptionPane.NO_OPTION){

                }
            }
        });

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);//添加菜单栏到主窗口

        JMenu menuBase = new JMenu("基础资料管理");
        menuBase.setFont(new Font("黑体",Font.PLAIN,14));
        menuBar.add(menuBase);


        JMenuItem itemFactory = new JMenuItem("读者管理");
        itemFactory.setFont(new Font("黑体",Font.PLAIN,14));
        itemFactory.addActionListener(this);
        menuBase.add(itemFactory); //向一级菜单添加二级菜单

        JMenuItem itemBuyer = new JMenuItem("借阅管理");
        itemBuyer.setFont(new Font("黑体",Font.PLAIN,14));
        itemBuyer.addActionListener(this);
        menuBase.add(itemBuyer); //向一级菜单添加二级菜单

        JMenuItem itemAccount = new JMenuItem("账号管理");
        itemAccount.setFont(new Font("黑体",Font.PLAIN,14));
        itemAccount.addActionListener(this);
        menuBase.add(itemAccount);

        JSeparator separator = new JSeparator();
        menuBase.add(separator);

        JMenuItem itemExit = new JMenuItem("退出登录");
        itemExit.setFont(new Font("黑体",Font.PLAIN,14));
        itemExit.addActionListener(this); //添加事件监听
        menuBase.add(itemExit); //向一级菜单添加二级菜单

        JMenu mnz = new JMenu("图书采购建议");
        mnz.setFont(new Font("黑体",Font.PLAIN,14));
        menuBar.add(mnz);

        JMenu mnu = new JMenu("关于我们");
        mnu.setFont(new Font("黑体",Font.PLAIN,14));
        menuBar.add(mnu);

        desktopPane = new JDesktopPane(); //桌面容器
        conenetPane.add(desktopPane,BorderLayout.CENTER);

        JPanel panel = new JPanel() ;
        conenetPane.add(panel,BorderLayout.SOUTH);
        JLabel labelTime = new JLabel();
        panel.add(labelTime);
        JLabel labelName = new JLabel();
        labelName.setText("当前用户【"+name+"】");
        panel.add(labelName);

        new Thread(){
            @Override
            public void run() {
                try {
                    while(true){
                        Thread.sleep(1000);
                        labelTime.setText(CommmonUtils.getCurrentTime());
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
//设置窗口最大化
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("退出登录")){
            int result = JOptionPane.showConfirmDialog(MainFrame.this,"确定退出系统？","系统提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
            if(result == JOptionPane.YES_OPTION){
                new LoginFrame().setVisible(true);
                MainFrame.this.dispose();
            }
        }else if(e.getActionCommand().equals("读者管理")){
            ReaderManager readManager=new ReaderManager();
            desktopPane.add(readManager);
            readManager.setVisible(true);

        }else if(e.getActionCommand().equals("借阅管理")){
            LibraryManage libraryManage=new LibraryManage();
            desktopPane.add(libraryManage);
            libraryManage.setVisible(true);

        }else if(e.getActionCommand().equals("账号管理")){
            AccountManager accountManager=new AccountManager();
            desktopPane.add(accountManager);
            accountManager.setVisible(true);

        }
    }
}